<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80"
      @submit="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }"
      @reset="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="项目名称" name="projectName">
            <t-input v-model="queryParams.projectName" clearable placeholder="请输入项目名称" style="width: 240px" />
          </t-form-item>
          <t-form-item label="项目编号" name="projectNum">
            <t-input v-model="queryParams.projectNum" clearable placeholder="请输入" style="width: 240px" />
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="addAction" variant="outline" theme="primary">
        <t-icon name="add" slot="icon" />
        添加
      </t-button>
    </div>
    <t-table rowKey="id" size="small" :data="data" :columns="columns" hover :pagination="pagination"
      :loading="dataLoading" :sort="sort" :hideSortTips="true" @sort-change="sortChange">
      <template #projectNum="{ row }">
        <div v-if="row.projectNum">
          {{ row.projectNum }}
        </div>
        <div v-else>
          暂未分配
        </div>
      </template>
      <template #adviceFunding="{ row }">
        {{ row.adviceFunding }}万元
      </template>
      <template #subsidyFunding="{ row }">
        {{ row.subsidyFunding }}万元
      </template>
      <template #projectName="{ row }">

        <t-link @click="dialog.objId = row.id; dialog.header = row.projectName; dialog.open_project_detail = true"
          hover="color" theme="primary">{{ row.projectName }}</t-link>
      </template>
      <template #orgMode="{ row }">
        {{ row.orgMode | filterByDict(dict.type.org_mode) }}
      </template>
      <template #projectType="{ row }">
        {{ row.projectType | filterByDict(dict.type.three_project_type) }}
      </template>
      <template #projectDomain="{ row }">
        {{ row.projectDomain | filterByDict(dict.type.three_project_domain) }}
      </template>
      <template #owningIndustry="{ row }">
        {{ row.owningIndustry | filterByDict(dict.type.owning_industry) }}
      </template>
      <template #owningSubject="{ row }">
        {{ row.owningSubject | filterByDict(dict.type.owning_subject) }}
      </template>
      <template #supportPlatform="{ row }">
        {{ row.supportPlatform | filterByDict(dict.type.support_platform) }}
      </template>
      <template #owningTeam="{ row }">
        {{ row.owningTeam | filterByDict(dict.type.owning_team) }}
      </template>

      <template #op="{ row }">
        <t-button @click="update(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon" />
          修改
        </t-button>
        <t-button size="small"
          @click="dialog.objId = row.id; dialog.header = row.projectName; dialog.open_project_detail = true"
          variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon" />
          详情
        </t-button>
        <t-button theme="danger" @click="delObj(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon" />
          删除
        </t-button>
      </template>
    </t-table>
    <project-detail-dialog tabDefaultValue="1" :header="dialog.header" :objId="dialog.objId"
      :visible.sync="dialog.open_project_detail" v-if="dialog.open_project_detail"></project-detail-dialog>


  </div>
</template>

<script>
import ProjectDetailDialog from "./components/three-project-detail-dialog.vue";

export default {
  name: "kjxm-pm-three-project-my",
  dicts: ["three_project_type", "org_mode", "three_project_domain", "owning_industry", "owning_subject", "support_platform", "owning_team"],
  components: {
    ProjectDetailDialog,
  },
  data () {
    return {
      site_tag: import.meta.env.VITE_SITE_TAG,
      sort: [],
      dialog: {
        open_project_detail: false,
        header: "",
        createUserId: '',
        objId: 0,
      },
      dictTypes: {},
      columns: [
        {
          colKey: "projectNum",
          title: "项目编号",
          align: "center",
          width: 120,
          // ellipsis: true,
        },
        {
          colKey: "projectName",
          title: "项目名称",
          align: "center",
          // ellipsis: true,
          width: 220
        },
        {
          colKey: "adviceFunding",
          title: "预计总投入",
          align: "center",
          width: 160,
        },
        {
          colKey: "subsidyFunding",
          title: "财政科技经费",
          align: "center",
          width: 140,
        },
        {
          colKey: "projectType",
          title: "项目类型",
          align: "center",
          width: 140,
        },
        {
          colKey: "orgMode",
          title: "组织方式",
          align: "center",
          width: 140,
        },
        {
          colKey: "projectDomain",
          title: "项目领域",
          align: "center",
          width: 140,
        },
        {
          colKey: "owningIndustry",
          title: "所属产业",
          align: "center",
          width: 140,
        },
        {
          colKey: "owningSubject",
          title: "所属学科",
          align: "center",
          width: 140,
        },
        {
          colKey: "supportPlatform",
          title: "依托平台",
          align: "center",
          width: 140,
        },
        {
          colKey: "owningTeam",
          title: "所属创新团队",
          align: "center",
          width: 140,
        },
        {
          colKey: "startDate",
          title: "项目开始日期",
          align: "center",
          width: 140,
        },
        {
          colKey: "endDate",
          title: "项目结束日期",
          align: "center",
          width: 140,
        },
        {
          colKey: "projectLeaderName",
          title: "项目负责人",
          align: "center",
          width: 100
        },
        {
          colKey: "declareDeptName",
          title: "承担单位",
          align: "center",
          width: 180,
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120,
          fixed: 'right'
        },
      ],
      data: [],
      dataLoading: false,
      queryParams: {

        pageNum: 1,
        pageSize: 10,
        projectName: '',
        projectNum: '',
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  computed: {
    ProjectStatusOptions () {
      let n
      if (this.dict.type.project_status) {
        n = [{ label: '全选', checkAll: true }].concat(this.dict.type.project_status)
      }
      return n
    }
  },
  async created () {

    this.getList();
    if (!this.$store.state.user.roles.includes('admin') && !this.$store.state.user.roles.includes('kjcspy')) {
      this.$api.kjxm.project.getZJZZPZ().then(res => {
      }).catch(e => {
        this.$message.error({ content: e.toString(), placement: 'center', closeBtn: true, duration: 0, style: { backgroundColor: "#fef0f0", color: 'black' } })
      })
    }

  },
  activated () {
    this.getList();

  },
  methods: {
    addAction () {
      this.$router.push(`/kjxm/other/pm/three-project-apply/${Date.parse(new Date())}`);
    },
    update (row) {
      this.$api.kjxm.threeProject.get(row.id).then(response => {
        const taskId = row.id;
        const activeFormList = [];
        sessionStorage.setItem(`${taskId}-projectId`, row.id);
        sessionStorage.setItem(`${taskId}-activeForm`, activeFormList[0]);
        if (row.isOldProject === '1') {
          this.$router.push({
            path: `/kjxm/other/pm/three-project-apply/${taskId}`,
            query: {
              title: response.data.projectName,
              projectStatus: row.projectStatus,
              isOldProject: '1',
            }
          });
        } else {
          this.$router.push({
            path: `/kjxm/other/pm/three-project-apply/${taskId}`,
            query: {
              title: response.data.projectName,
              projectStatus: row.projectStatus
            }
          });
        }

      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    delObj (row) {
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除姓名为"${row.projectName}"的数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.kjxm.threeProject.del(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        }
      });
    },

    sortChange (sort) {
      // this.sort = sort;
      // let orderByColumn = []
      // let isAsc = []
      // if (sort instanceof Array) {
      //   orderByColumn = sort.map(val => val.sortBy)
      //   isAsc = sort.map(val => val.descending ? `desc` : `asc`)
      // } else {
      //   orderByColumn = [sort.sortBy]
      //   isAsc = [sort.descending ? `desc` : `asc`]
      // }
      // this.queryParams.orderByColumn = orderByColumn.join(",")
      // this.queryParams.isAsc = isAsc.join(",")
      // this.getList();
    },
    getList () {
      this.dataLoading = true;
      this.$api.kjxm.threeProject.myList(this.queryParams).then(response => {
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },

  }
}
</script>

<style scoped></style>
